//
//  HomeRecomedController.m
//  OnionHome
//
//  Created by fengjie on 2019/9/16.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "HomeRecomedController.h"
#import "HomeRecomedModel.h"

@interface HomeRecomedController ()<UITableViewDelegate,UITableViewDataSource>
/**listView*/
@property (nonatomic,strong)UITableView*listView;
/**<#属性#>*/
@property (nonatomic,strong)NSMutableArray*dataArray;
@end

@implementation HomeRecomedController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.listView];
    [self.listView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    [self setNotificationCenter];
}
-(void)setNotificationCenter{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadLocaData) name:K_ReleaseArticle object:nil];
}
-(void)reloadLocaData{
    self.dataArray = nil;
    [self.listView reloadData];
}
#pragma mark --UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HomeRecomedCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([HomeRecomedCell class])];
    
    RecomedLeftTextRightImageCell * imageCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([RecomedLeftTextRightImageCell class])];
    HomeRecomedModel * model = self.dataArray[indexPath.row];
    
    if (model.imageArray.count == 1) {
        imageCell.recmode = model;
        return imageCell;
    }
    cell.recmode = model;
    
//
//    if (indexPath.row == 2) {
//        imageCell.isTop = NO;
//        return imageCell;
//    }
//    if (indexPath.row>1) {
//        cell.isTop = NO;
//    }else{
//        cell.isTop = YES;
//    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ImageAndTextDetailController * vc = [ImageAndTextDetailController new];
//    HTMLViewController * vc = [HTMLViewController new];
    vc.model = self.dataArray[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
}
-(UITableView*)listView{
    if(!_listView){
        _listView=[[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _listView.delegate = self;
        _listView.dataSource = self;
        _listView.tableFooterView = UIView.new;
        _listView.backgroundColor = UIColorFromRGB(0xE9EEE7);
        _listView.rowHeight = UITableViewAutomaticDimension;
        _listView.estimatedRowHeight = 44.0f;

        [_listView setSeparatorInset:UIEdgeInsetsMake(0,0, 0, 0)];
        [_listView registerClass:[HomeRecomedCell class] forCellReuseIdentifier:NSStringFromClass([HomeRecomedCell class])];
        [_listView registerClass:[RecomedLeftTextRightImageCell class] forCellReuseIdentifier:NSStringFromClass([RecomedLeftTextRightImageCell class])];
        _listView.separatorColor = SeparatorColor;

    }
    return _listView;
}

-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
        
        NSMutableArray * oneArray = [PlistTool getPlistType:ReleseArticle];
        
        [oneArray enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            HomeRecomedModel *model = [HomeRecomedModel new];
            [model setValuesForKeysWithDictionary:obj];
            [_dataArray addObject:model];
        }];

        
        
        NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"HomeRecomed" ofType:@"plist"];
           //当数据结构为数组时
        NSArray *array = [[NSArray alloc] initWithContentsOfFile:plistPath];
        [array enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            HomeRecomedModel *model = [HomeRecomedModel new];
            [model setValuesForKeysWithDictionary:obj];
            [_dataArray addObject:model];
        }];

    }
    return _dataArray;
}

@end

@interface HomeRecomedCell ()
/**标题*/
@property (nonatomic,strong)UILabel*titleLabel;
/**置顶文字*/
@property (nonatomic,strong)UILabel*topLabel;
/**类型、评论数、时间*/
@property (nonatomic,strong)UILabel*numberLabel;

@end

@implementation HomeRecomedCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self createUI];
    }
    return self;
}
-(void)setIsTop:(BOOL)isTop{
    _isTop = isTop;
    self.topLabel.hidden = !isTop;
    if (isTop) {
        [self.numberLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(48);
            make.centerY.mas_equalTo(self.topLabel);
            make.right.mas_equalTo(-30);
        }];
    }else{
        [self.numberLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(16);
            make.centerY.mas_equalTo(self.topLabel);
            make.right.mas_equalTo(-30);
        }];
    }
}
-(void)setRecmode:(HomeRecomedModel *)recmode{
    _recmode  = recmode;
    [self setIsTop:recmode.isTop];
//    self.titleLabel.text = recmode.title;
    [TitleTool setDynamicTitle:recmode.title Label:self.titleLabel];

}
-(void)createUI{
    UIButton * deleButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [deleButton setBackgroundImage:IMAGE_NAMED(@"icon_quxiao") forState:UIControlStateNormal];
    kWeakSelf(weakSelf);
    [deleButton addActionHandler:^{
        PresentationObject * object = [PresentationObject sharedPresentationObject];
        CGFloat y = (KScreenHeight - 290)/2;
//            object.alpha_bgView = 0.3;
        object.subViewFrame =CGRectMake(20, y, KScreenWidth-40, 290);
        OneToastController * vc = [OneToastController new];
        [object showPresentingViewController:[weakSelf getCurrentViewController] PresentedViewController:[[BasseNavigationController alloc]initWithRootViewController:vc]];
        vc.notInterestedBlock = ^{
            
        };
    }];

    
    [self addSubview:self.titleLabel];
    [self addSubview:self.topLabel];
    [self addSubview:self.numberLabel];
    [self addSubview:deleButton];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.top.mas_equalTo(16);
        make.right.mas_equalTo(-16);
    }];
    [self.topLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.titleLabel);
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(10);
        make.width.mas_equalTo(28);
        make.height.mas_equalTo(16);
        make.bottom.mas_equalTo(-16);
    }];
    [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.topLabel.mas_right).offset(4);
        make.centerY.mas_equalTo(self.topLabel);
        make.right.mas_equalTo(-30);
    }];
    
    [deleButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-16);
        make.centerY.mas_equalTo(self.topLabel);
    }];
}
#pragma mark ---懒加载

-(UILabel*)titleLabel{
    if(!_titleLabel){
        _titleLabel=[UILabel new];
        _titleLabel.textColor = UIColorFromRGB(0x273323);
        _titleLabel.font = PingFangSC_Medium(18);
        _titleLabel.text = @"现代人大多处于亚健康，与饮食的关系？与饮食的关系？";
        _titleLabel.numberOfLines = 2;
    }
    return _titleLabel;
}
-(UILabel*)topLabel{
    if(!_topLabel){
        _topLabel=[UILabel new];
        _topLabel.textColor = UIColorFromRGB(0xFF7300);
        _topLabel.font = PingFangSC_Medium(11);
        _topLabel.text = @"置顶";
        _topLabel.layer.borderWidth = 1;
        _topLabel.textAlignment = NSTextAlignmentCenter;
        _topLabel.layer.borderColor =UIColorFromRGB(0xFF7300).CGColor;

    }
    return _topLabel;
}
-(UILabel*)numberLabel{
    if(!_numberLabel){
        _numberLabel=[UILabel new];
        _numberLabel.textColor = UIColorFromRGB(0x959E97);
        _numberLabel.font = SYSTEMFONT(12);
        _numberLabel.text = @"葱花之家 312评论 30分钟前";
    }
    return _numberLabel;
}
@end

@interface RecomedLeftTextRightImageCell ()

/**标题*/
@property (nonatomic,strong)UILabel*titleLabel;
/**置顶文字*/
@property (nonatomic,strong)UILabel*topLabel;

/**右边图片*/
@property (nonatomic,strong)UIImageView*rightImage;
@end

@implementation RecomedLeftTextRightImageCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self createUI];
    }
    return self;
}
-(void)setIsTop:(BOOL)isTop{
    _isTop = isTop;
    self.topLabel.hidden = !isTop;
    if (isTop) {
        [self.numberLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(48);
            make.centerY.mas_equalTo(self.topLabel);
            make.right.mas_equalTo(-30);
        }];
    }else{
        [self.numberLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(16);
            make.centerY.mas_equalTo(self.topLabel);
            make.right.mas_equalTo(-30);
        }];
    }
}
-(void)setRecmode:(HomeRecomedModel *)recmode{
    _recmode = recmode;
    [self setIsTop:recmode.isTop];
//    self.titleLabel.text = recmode.title;
    [TitleTool setDynamicTitle:recmode.title Label:self.titleLabel];
    self.rightImage.image = IMAGE_NAMED(recmode.imageArray.lastObject);
}
-(void)setHomeCityModel:(HomeCityModel *)homeCityModel{
    _homeCityModel = homeCityModel;
    [self setIsTop:NO];
//    self.titleLabel.text = homeCityModel.title;
    [TitleTool setDynamicTitle:homeCityModel.title Label:self.titleLabel];
    self.rightImage.image = homeCityModel.imageArray.lastObject;

}
-(void)setIndusModel:(HomeIndustryDetailsModel *)indusModel{
    _indusModel = indusModel;
    [self setIsTop:NO];
//    self.titleLabel.text = indusModel.title;
    [TitleTool setDynamicTitle:indusModel.title Label:self.titleLabel];

    self.rightImage.image = indusModel.imageArray.lastObject;

}
-(void)setQamodel:(HomeQuestionAnswerModel *)qamodel{
    _qamodel = qamodel;
    [self setIsTop:NO];
//    self.titleLabel.text = qamodel.title;
    [TitleTool setDynamicTitle:qamodel.title Label:self.titleLabel];

    self.rightImage.image = qamodel.imagesArray.lastObject;

}
-(void)createUI{
    UIButton * deleButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [deleButton setBackgroundImage:IMAGE_NAMED(@"icon_quxiao") forState:UIControlStateNormal];
    kWeakSelf(weakSelf);
    [deleButton addActionHandler:^{
        PresentationObject * object = [PresentationObject sharedPresentationObject];
        CGFloat y = (KScreenHeight - 290)/2;
//            object.alpha_bgView = 0.3;
        object.subViewFrame =CGRectMake(20, y, KScreenWidth-40, 290);
        OneToastController * vc = [OneToastController new];
        [object showPresentingViewController:[weakSelf getCurrentViewController] PresentedViewController:[[BasseNavigationController alloc]initWithRootViewController:vc]];
        vc.notInterestedBlock = ^{
            
        };
    }];
    
    [self addSubview:self.titleLabel];
    [self addSubview:self.rightImage];
    [self addSubview:self.topLabel];
    [self addSubview:self.numberLabel];
    [self addSubview:deleButton];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.top.mas_equalTo(16);
        make.right.mas_equalTo(-136);
    }];
    [self.rightImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-16);
        make.width.mas_equalTo(112);
        make.height.mas_equalTo(84);
        make.top.mas_equalTo(self.titleLabel);
    }];
    [self.topLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.titleLabel);
        make.top.mas_equalTo(self.rightImage.mas_bottom).offset(10);
        make.width.mas_equalTo(28);
        make.height.mas_equalTo(16);
        make.bottom.mas_equalTo(-16);
    }];
    [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.topLabel.mas_right).offset(4);
        make.centerY.mas_equalTo(self.topLabel);
        make.right.mas_equalTo(-30);
    }];
    
    [deleButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-16);
        make.centerY.mas_equalTo(self.topLabel);
    }];

}
#pragma mark ---懒加载
-(UILabel*)titleLabel{
    if(!_titleLabel){
        _titleLabel=[UILabel new];
        _titleLabel.textColor = UIColorFromRGB(0x273323);
        _titleLabel.font = PingFangSC_Medium(18);
        _titleLabel.text = @"做烤鸡的时候，千万别直接放进烤箱，多加一步，鸡肉更加入味";
        _titleLabel.numberOfLines = 3;
    }
    return _titleLabel;
}
-(UILabel*)topLabel{
    if(!_topLabel){
        _topLabel=[UILabel new];
        _topLabel.textColor = UIColorFromRGB(0xFF7300);
        _topLabel.font = PingFangSC_Medium(11);
        _topLabel.text = @"置顶";
        _topLabel.layer.borderWidth = 1;
        _topLabel.textAlignment = NSTextAlignmentCenter;
        _topLabel.layer.borderColor =UIColorFromRGB(0xFF7300).CGColor;
        
    }
    return _topLabel;
}
-(UILabel*)numberLabel{
    if(!_numberLabel){
        _numberLabel=[UILabel new];
        _numberLabel.textColor = UIColorFromRGB(0x959E97);
        _numberLabel.font = SYSTEMFONT(12);
        _numberLabel.text = @"广州当地美食 312评论 30分钟前";
    }
    return _numberLabel;
}
-(UIImageView*)rightImage{
    if(!_rightImage){
        _rightImage=[UIImageView new];
        _rightImage.image = IMAGE_NAMED(@"dynamicDefut");
        
    }
    return _rightImage;
}
@end
